package com.wanmait.jd.utils;

import com.wanmait.jd.service.ItemService;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;

@Component
public class HttpUtils {
    private PoolingHttpClientConnectionManager cm;
    public HttpUtils() {
        this.cm = new PoolingHttpClientConnectionManager();
        //设置最大连接数
        this.cm.setMaxTotal(100);
        //设置每个主机的最大连接数
        this.cm.setDefaultMaxPerRoute(10);


    }

    //根据请求地址下载页面
    public String doGet(String url) throws Exception {
        //获取httpClient对象
        CloseableHttpClient client = HttpClients.custom().setConnectionManager(this.cm).build();
        //设置httpGet请求对象，设置URL地址
        HttpGet get = new HttpGet(url);
        //设置请求信息
        get.setConfig(this.getConfig());
        setHeaders(get);
        CloseableHttpResponse response =null;
        try {
            //使用httpClie发起请求，获取响应
             response = client.execute(get);
            //解析响应，返回结果
            if (response.getStatusLine().getStatusCode()==200){
                //判断响应体Entity是否不为空，如果不为空就可以使用EntityUtils
                if (response.getEntity()!=null){
                    String conter = EntityUtils.toString(response.getEntity(), "utf8");
                    return conter;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (response!=null){
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return "";

    }
    //下载图片
    public String doGetImagera(String url) throws Exception
    {
        //获取httpClient对象
        CloseableHttpClient client = HttpClients.custom().setConnectionManager(this.cm).build();
        //设置httpGet请求对象，设置URL地址
        HttpGet get = new HttpGet(url);
        //设置请求信息
        get.setConfig(this.getConfig());
        CloseableHttpResponse response =null;
        try {
            //使用httpClie发起请求，获取响应
            response = client.execute(get);
            //解析响应，返回结果
            if (response.getStatusLine().getStatusCode()==200){
                //判断响应体Entity是否不为空，如果不为空就可以使用EntityUtils
                if (response.getEntity()!=null){
                    //下载图片
                    //获取图片后缀
                    String extName = url.substring(url.lastIndexOf("."));
                    //创建图片名，重命名图片
                    String picName = UUID.randomUUID().toString()+extName;
                    //                    //下载图片
                    //声明OutPutStream
                    OutputStream outputStream = new FileOutputStream(new File("D:\\pachong"+picName));
                    response.getEntity().writeTo(outputStream);
                    //                    //返回图片名称
                    return picName;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (response!=null){
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //如果下载失败，返回空字符串
        return "";


    }

    //设置请求信息
    private RequestConfig getConfig() {
        RequestConfig config = RequestConfig.custom()
                .setConnectTimeout(1000) //创建连接的最长时间
                .setConnectionRequestTimeout(500)//获取连接的最长时间
                .setSocketTimeout(10*1000)//数据传输的最长时间
                .build();
        return config;
    }

    private void setHeaders(HttpGet httpGet) {
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2");
    }

}
